<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="/common/session.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<LINK type="text/css" rel="stylesheet" href="${ctx}/css/style.css"/>
<script type="text/javascript" src="${ctx}/js/xtable.js"></script>
<script type="text/javascript" src="${ctx}/js/xtree.js"></script>
<script type="text/javascript" src="${ctx}/js/validator.js"></script>
<script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
<script type="text/javascript" src="${ctx}/dwr/util.js"></script>
<script type="text/javascript" src="${ctx}/dwr/interface/SecurityService.js"></script>
<script type="text/javascript" src="${ctx}/js/loading.js"></script>
</head>
<body onload="init()">
<table>
    <tr>
        <td valign="top">
            <table>
                <tr>
                    <td>
                        <form id="accountForm">
                        <table style="width:100%">
                            <tr>
                                <td>
                                    账户名
                                </td>
                                <td>
                                    <input type="text" id="accountName" elname="账户名" required="1" maxLen="100"/>
                                </td>
                                <td>
                                    账号备注
                                </td>
                                <td>
                                    <input type="text" id="accountRemark" elname="账户姓名" required="1" maxLen="100"/>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    账户密码
                                </td>
                                <td>
                                    <input type="password" id="accountPassword1" elname="账户密码" required="1" maxLen="100"/>
                                </td>
                                <td>
                                    确认密码
                                </td>
                                <td>
                                    <input type="password" id="accountPassword2" elname="确认密码" required="1" maxLen="100"/>
                                </td>
                            </tr>
                              <tr>
                                <td>
                                    是否有效
                                </td>
                                <td>
                                    <select id="accountState" name="accountState" elname="是否有效">
                                        <option value="1">有效</option>
                                        <option value="0">无效</option>
                                    </select>
                                </td>
                           </tr>
                           <tr>
                                <td align="center" colspan="6">
                                    <img src="${ctx}/images/common/button_select.jpg" title="查询账户" style="cursor:pointer" onclick="getAccountPage()"/>
                                    <img src="${ctx}/images/common/button_insert.jpg" title="添加账户" style="cursor:pointer" onclick="insertAccount()"/>
                                    <img src="${ctx}/images/common/button_update.jpg" title="修改账户" style="cursor:pointer" onclick="updateAccount()"/>
                                    <img src="${ctx}/images/common/button_delete.jpg" title="删除账户" style="cursor:pointer" onclick="deleteAccount()"/>
                                    <img src="${ctx}/images/common/button_reset.jpg" title="清空账户" style="cursor:pointer" onclick="$('accountForm').reset()"/>
                                </td>
                            </tr>
                        </table>
                        </form>
                    </td>
                </tr>
                <tr>
                    <td>
                        <SCRIPT LANGUAGE="JavaScript" type="">
                        var accountThead = new Array(
                        new XThead("选择", "radio"),
                        new XThead("账户"),
                        new XThead("备注"),
                        new XThead("操作")
                        );

                        var accountXTable = new XTable("account", accountThead);

                        accountXTable.XTbodyArray = function (model) {
                            var array = new Array(
                            new XTbody("account", model.accountId, "radio", "onclick='setAccount(this)'", model),
                            new XTbody("account", model.accountName),
                            new XTbody("account", model.accountRemark),
                            new XTbody("account", "<a style='cursor:hand' onclick='getProperty(this)'>扩展属性维护</a>")
                            );
                            return array;
                        }

                        accountXTable.getXList = function (page){
                            var account = new Account();
                            account.accountName = dwr.util.getValue("accountName");
                            account.accountRemark = dwr.util.getValue("accountRemark");
                            account.accountState = dwr.util.getValue("accountState");
                            account.pageIndex = page;
                            account.pageSize = accountXTable.pageSize;
                            SecurityService.selectAccountList(account, function(list){accountXTable.addTBody(list);});
                        }

                        function getAccountPage(){
                            var account = new Account();
                            account.accountName = dwr.util.getValue("accountName");
                            account.accountRemark = dwr.util.getValue("accountRemark");
                            account.accountState = dwr.util.getValue("accountState");
                            SecurityService.selectAccountNum(account, function(num){accountXTable.setTotalPage(num);});
                        }
                        </SCRIPT>
                    </td>
                </tr>
            </table>
        </td>
        <td>
        &nbsp;
        </td>
        <td valign="top">
            <SCRIPT language="javascript">
            var roleThead = new Array(
            new XThead("选择", "checkbox"),
            new XThead("角色名称")
            );

            var roleXTable = new XTable("role", roleThead, true);

            roleXTable.XTbodyArray = function (model) {
                var array = new Array(
                new XTbody("role", model.roleId, "checkbox"),
                new XTbody("role", model.roleName)
                );
                return array;
            }

            roleXTable.getXList = function (page){
                var role = new Role();
//                role.pageIndex = page;
//                role.pageSize = roleXTable.pageSize;
                SecurityService.selectRoleList(role, function(list){roleXTable.addTBody(list);});
            }
            function getRolePage(){
                var role = new Role();
                SecurityService.selectRoleNum(role, function(num){roleXTable.setTotalPage(num);}) ;
            }
            </SCRIPT>
        </td>
    </tr>
</table>
<div id="propertyDiv" style="left:150px;top:120px;width:600px;border:1px solid #c9c9c9;display:none;position: absolute;background:white">
    <table>
        <thead class="xThead">
            <tr>
                <td style="border-right:0px">&nbsp;
                    <img alt="" src="${ctx}/images/xwin/xwin_title_head.png"/>
                                                                      扩展属性维护
                </td>
                <td align="right" width="35" style="border-left:0px">
                    <img alt="关闭" src="${ctx}/images/xwin/xwin_close.gif" onclick="hiddenPropertyDiv()"/>
                    &nbsp;
                </td>
            </tr>
        </thead>
    </table>
    <form id="accountPropertyForm" name="accountPropertyForm">
        <table>
            <tr>
                <td nowrap>
                    属性名称
                </td>
                <td align="left">
                    <INPUT TYPE="text" id="propertyKey" elname="属性名称" required="1" maxLen="100" />
                </td>
                <td nowrap>
                    属性值
                </td>
                <td>
                    <INPUT TYPE="text" id="propertyValue" elname="属性值" required="1" maxLen="100" />
                    <INPUT TYPE="hidden" id="accountId" />
                </td>
                <td>
                    <button onclick="insertProperty()">新增</button>
                    <button onclick="updateProperty()">修改</button>
                    <button onclick="deleteProperty()">删除</button>
                </td>
            </tr>
        </table>
    </form>

    <SCRIPT LANGUAGE="JavaScript" type="">
    var propertyThead = new Array(
    new XThead("选择", "radio"),
    new XThead("属性名称"),
    new XThead("属性值")
    );

    var propertyXTable = new XTable("property", propertyThead, true);

    propertyXTable.XTbodyArray = function (model) {
        var array = new Array(
        new XTbody("property", model.accountId, "radio", "onclick='setProperty(this)'", model),
        new XTbody("property", model.propertyKey),
        new XTbody("property", model.propertyValue)
        );
        return array;
    }

    propertyXTable.getXList = function (page){
        var property = new Property();
        property.accountId = dwr.util.getValue("accountId");
        SecurityService.selectAccountPropertyList(property, function(list){propertyXTable.addTBody(list);});
    }
    </SCRIPT>
</div>
</body>
</html>
<script type="text/javascript">
function init(){
    getAccountPage();
    roleXTable.goPage(1);
}

function setAccount(radio){
    var account = radio.parentElement.parentElement.model;
    dwr.util.setValue("accountName", account.accountName);
//    dwr.util.setValue("accountPassword1", account.accountPassword);
//    dwr.util.setValue("accountPassword2", account.accountPassword);
    dwr.util.setValue("accountRemark", account.accountRemark);
    dwr.util.setValue("accountState", account.accountState);
    var accountRole = new AccountRole();
    accountRole.accountId = account.accountId;
    SecurityService.selectAccountRoleList(accountRole, callbackSelectAccountRole);
}

function callbackSelectAccountRole(list){
    var roleIds = "";
    for(var i = 0; i < list.length; i++){
        roleIds += list[i].roleId + ",";
    }
    if(roleIds.length > 0){
        roleIds = roleIds.substring(0, roleIds.length - 1);
    }
    roleXTable.setCheckBoxValue(roleIds);
}

function insertAccount(){
    var _form = $("accountForm");
    if(!validate(_form)){
        return false;
    }
    var accountPassword1 = dwr.util.getValue("accountPassword1");
    var accountPassword2 = dwr.util.getValue("accountPassword2");
    if(accountPassword1 != accountPassword2){
        alert("两次输入的密码不一致，请重新输入");
        return false;
    }
    var account = new Account();
    account.accountName = dwr.util.getValue("accountName");
    account.pageIndex = 1;
    account.pageSize = 1;
    SecurityService.selectAccountList(account, callbackSelectAccountList) ;
}

function callbackSelectAccountList(list){
    if(list.length > 0){
        alert("相同的账户名不允许重复添加！");
        callbackAccount();
        return false;
    }

    var account = new Account();
    account.accountName = dwr.util.getValue("accountName");
    account.accountPassword = dwr.util.getValue("accountPassword1");
    account.accountRemark = dwr.util.getValue("accountRemark");
    account.accountState = dwr.util.getValue("accountState");
    var roleIds = roleXTable.getCheckBoxValue();
    SecurityService.insertAccount(account, roleIds, callbackAccount);
}

function updateAccount(){
    var _form = $("accountForm");
    var accountPassword = dwr.util.getValue("accountPassword1");
    var accountPassword2 = dwr.util.getValue("accountPassword2");
    if(accountPassword != accountPassword2){
        alert("两次输入的密码不一致，请重新输入");
        return false;
    }

    var accountId = accountXTable.getRadioValue();
    if(accountId == null){
        alert("请选择要修改的账户");
        return false;
    }

    if(!confirm("你确定修改此账户信息吗？")){
        return false;
    }

    var account = new Account();
    account.accountId = accountId;
    if(accountPassword.length > 0){
        account.accountPassword = accountPassword;
    }
    account.accountRemark = dwr.util.getValue("accountRemark");
    account.accountState = dwr.util.getValue("accountState");
    SecurityService.updateAccount(account, callbackAccount);
    var roleIds = roleXTable.getCheckBoxValue();
    var accountRole = new AccountRole();
    accountRole.accountId = account.accountId;
    SecurityService.insertAccountRole(accountRole, roleIds);
}


function deleteAccount(){
    var accountId = accountXTable.getRadioValue();
    if(!accountId){
        return false;
    }
    var account = new Account();
    account.accountId = accountId;
    SecurityService.deleteAccount(account, callbackAccount);
}

function callbackAccount(){
    getAccountPage();
    $("accountForm").reset();
    document.getElementById("accountName").focus();
}

function getProperty(radio){
    showPropertyDiv();
    var obj = radio.parentElement.parentElement.model;
    dwr.util.setValue("accountId", obj.accountId);
    propertyXTable.goPage(1);
}

function showPropertyDiv(){
    $("propertyDiv").style.display = "block";
    $("accountPropertyForm").reset();
}

function hiddenPropertyDiv(){
    $("propertyDiv").style.display = "none";
}

function setProperty(radio){
    var property = radio.parentElement.parentElement.model;
    dwr.util.setValue("accountId", property.accountId);
    dwr.util.setValue("propertyKey", property.propertyKey);
    dwr.util.setValue("propertyValue", property.propertyValue);
}

function insertProperty(){
    var _form = $("accountPropertyForm");

    if(!validate(_form)){
        return false;
    }

    var property = new Property();
    property.accountId = dwr.util.getValue("accountId");
    property.propertyKey = dwr.util.getValue("propertyKey");
    property.propertyValue = dwr.util.getValue("propertyValue");
    SecurityService.insertAccountProperty(property, callbackProperty);
}

function updateProperty(){
    var _form = $("accountPropertyForm");
    if(!validate(_form)){
        return false;
    }

    if(propertyXTable.getRadioValue() != null){
        if(!confirm("你确定要修改此扩展属性")){
            return false;
        }
        var property = new Property();
        property.accountId = dwr.util.getValue("accountId");
        property.propertyKey = dwr.util.getValue("propertyKey");
        property.propertyValue = dwr.util.getValue("propertyValue");
        SecurityService.updateAccountProperty(property, callbackProperty);
    }
    else{
        alert("请你选择要修改的扩展属性");
    }
}

function deleteProperty(){
    if(propertyXTable.getRadioValue() != null){
        if(!confirm("你确定删除此扩展属性")){
            return false;
        }
        var property = new Property();
        property.accountId = dwr.util.getValue("accountId");
        property.propertyKey = dwr.util.getValue("propertyKey");
        SecurityService.deleteAccountProperty(property, callbackProperty);
    }
    else{
        alert("请你选中要删除的扩展属性");
    }
}

function callbackProperty(){
    propertyXTable.goPage(1);
}
</script>
